package cn.iocoder.yudao.module.famous.doctor.service.archives;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.famous.doctor.controller.admin.archives.vo.*;
import cn.iocoder.yudao.module.famous.doctor.dal.dataobject.archives.ArchivesDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.famous.doctor.convert.archives.ArchivesConvert;
import cn.iocoder.yudao.module.famous.doctor.dal.mysql.archives.ArchivesMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.famous.doctor.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 患者档案 Service 实现类
 *
 * @author 系统管理员
 */
@Service
@Validated
public class ArchivesServiceImpl implements ArchivesService {

    @Resource
    private ArchivesMapper archivesMapper;

    @Override
    public Long createArchives(ArchivesCreateReqVO createReqVO) {
        // 插入
        ArchivesDO archives = ArchivesConvert.INSTANCE.convert(createReqVO);
        archivesMapper.insert(archives);
        // 返回
        return archives.getId();
    }

    @Override
    public void updateArchives(ArchivesUpdateReqVO updateReqVO) {
        // 校验存在
        validateArchivesExists(updateReqVO.getId());
        // 更新
        ArchivesDO updateObj = ArchivesConvert.INSTANCE.convert(updateReqVO);
        archivesMapper.updateById(updateObj);
    }

    @Override
    public void deleteArchives(Long id) {
        // 校验存在
        validateArchivesExists(id);
        // 删除
        archivesMapper.deleteById(id);
    }

    private void validateArchivesExists(Long id) {
        if (archivesMapper.selectById(id) == null) {
            throw exception(ARCHIVES_NOT_EXISTS);
        }
    }

    @Override
    public ArchivesDO getArchives(Long id) {
        return archivesMapper.selectById(id);
    }

    @Override
    public List<ArchivesDO> getArchivesList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return archivesMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<ArchivesDO> getArchivesPage(ArchivesPageReqVO pageReqVO) {
        return archivesMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ArchivesDO> getArchivesList(ArchivesExportReqVO exportReqVO) {
        return archivesMapper.selectList(exportReqVO);
    }

    @Override
    public Long saveArchives(ArchivesUpdateReqVO createReqVO) {
//        archivesMapper.saveOrUpdateBatch((Collection<ArchivesDO>) createReqVO);
        ArchivesDO updateObj =ArchivesConvert.INSTANCE.convert(createReqVO);

        if (createReqVO.getId()!=null && createReqVO.getId()>0) {
            //修改
            archivesMapper.updateById(updateObj);
        }else{
            //添加
            archivesMapper.insert(updateObj);
        }
        return 1L;
    }

}
